Skip to main content

approveAppointmentRating

approveAppointmentRating

This method handles the approval or rejection of an appointment rating, ensuring only authorized agents can perform the action, and updates related rating statistics accordingly.

  1. Initialize Transaction

    • Use an existing QueryRunner if provided, or create a new one.
    • Start a database transaction.
  2. Validate Approving Agent

    • Retrieve the agent with the given agent_id.
    • Confirm the agent exists and has the appropriate role (AgentSuperAdmin or AgentAdmin).
    • If the agent is not found or unauthorized, throw a NotFoundException.
  3. Retrieve Appointment Rating

    • Find the rating record on rating_appointment table by booking_id.
    • Load related entities: agent, business location, and who approved it.
    • If no rating is found, throw a NotFoundException.
  4. Check Rating Status

    • Ensure the rating’s current status is 'pending'.
    • Confirm the rating has not already been approved (approvedBy is null).
    • If the rating is not pending or already approved/rejected, throw a ConflictException.
  5. Approve or Reject Rating

    • Based on the status in the input DTO:

      • If 'approve', call the rating’s approve method, passing the approving agent.
      • If 'reject', call the rating’s reject method, passing the approving agent.
  6. Save Rating Update

    • Persist the updated rating record within the transaction.
  7. Update Related Ratings (On Approval Only)

    • If approved, update related ratings statistics:

      • Provider rating statistics
      • Staff rating statistics
      • Clinic rating statistics
    • Use a shared updateRating helper function with relevant repositories.

  8. Commit or Rollback Transaction

    • Commit the transaction if all steps succeed.
    • Rollback the transaction if any error occurs during processing.
  9. Release Query Runner

    • Release database resources held by the QueryRunner.
  10. Return Result

    • Return a success response indicating whether the rating was approved or rejected.